<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@include file="/common/session.jsp" %>
<c:set var="accountId" value="${param.accountId}"/>
<c:set var="accountName" value="${param.accountName}"/>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=2.0, user-scalable=no, width=device-width">
    <LINK type="text/css" rel="stylesheet" href="${ctx}/css/default.css"/>
    <script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/util.js"></script>
    <script type='text/javascript' src='${ctx}/dwr/interface/SecurityRpcService.js'></script>
    <script type="text/javascript" src="${ctx}/js/loading.js"></script>
</head>
<body>
<br>
<form id="accountForm">
    <center>
        <table width="100%" cellpadding="5" cellspacing="5">
            <tr align="center">
                <td>
                    原始密码：&nbsp;&nbsp;<input placeholder="输入当前账户密码" type="password" id="accountPassword" name="accountPassword" style="width:400px;text-align:center;" required/>
                </td>
            </tr>
            <tr align="center">
                <td>
                    &nbsp;&nbsp;&nbsp;新密码：&nbsp;&nbsp;<input placeholder="输入新密码" type="password" id="newAccountPassword" name="newAccountPassword" style="width:400px;text-align:center;" required/>
                </td>
            </tr>
            <tr align="center">
                <td>
                    重复密码：&nbsp;&nbsp;<input placeholder="重复输入新密码" type="password" id="againAccountPassword" name="againAccountPassword" style="width:400px;text-align:center;" required/>
                </td>
            </tr>
            <tr>
                <td style="border-bottom: 1px solid #e1ecf5" colspan="6">
                </td>
            </tr>
            <tr>
                <td colspan="6" align="center">
                    <button type="button" onclick="selectPassword()">保存</button>
                    <button type="button" onclick="deleteForm()">清空</button>
                </td>
            </tr>
        </table>
    </center>
</form>
</body>
</html>

<script LANGUAGE="JavaScript" >

    function selectPassword() {
        if (!validate($("accountForm"))) {
            return false;
        }
        var accountPassword = dwr.util.getValue("accountPassword");
        var newAccountPassword = dwr.util.getValue("newAccountPassword");
        var againAccountPassword = dwr.util.getValue("againAccountPassword");
        if(newAccountPassword != againAccountPassword){
            alert("新密码两次输入的不一致，请重新输入!");
            return false;
        }
        var account = new Account();
        account.accountId = sessionStorage.getItem("accountId");
        account.accountName = sessionStorage.getItem("accountName");
        SecurityRpcService.selectAccountList(account, callbackSelectAccountList);
    }

    function callbackSelectAccountList(list){
        if(list.length == 1){
            var account = list[0];
            var accountPasswordEncryption = account.accountPassword;
            var accountPassword = dwr.util.getValue("accountPassword");
            SecurityRpcService.validateAccountPassword(accountPassword, accountPasswordEncryption, callbackValidateAccountPassword);
        }
    }

    function callbackValidateAccountPassword(flag){
        if(!flag){
            alert("密码输入有误，请重新输入！");
            return false;
        }else{
            updatePassword();
        }
    }

    function updatePassword(){
        if (!validate($("accountForm"))) {
            return false;
        }
        var account = new Account();
        account.accountId = sessionStorage.getItem("accountId");
        account.accountPassword = dwr.util.getValue("newAccountPassword");
        SecurityRpcService.updateAccount(account, callbackUpdateAccount);
    }

    function callbackUpdateAccount(){
        alert("修改成功！");
    }

    function deleteForm(){
        $("accountForm").reset();
    }
</script>
